//
//  NSObject+DFLayoutSupport.m
//  Pods
//
//  Created by rujia chen on 17/4/17.
//
//

#import "NSObject+DFLayoutSupport.h"
#import "NSObject+DFSwizzle.h"
#import <objc/runtime.h>

const char *kPropertyLength;

@implementation NSObject (DFLayoutSupport)

- (void)df_setLayoutLength:(CGFloat)length {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError *error;
        [self.class df_swizzleMethod:@selector(length) withMethod:@selector(_length) error:&error];
//        NSLog(@"a");
    });
    objc_setAssociatedObject(self, &kPropertyLength, @(length), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)_length {
    CGFloat length = [objc_getAssociatedObject(self, &kPropertyLength) floatValue];
    return length?:[self _length];
}

@end
